package com.jf.apply_car.controller;

import com.jf.apply_car.bean.Driver;
import com.jf.apply_car.bean.Userinfo;
import com.jf.apply_car.service.UserinfoService;
import com.jf.apply_car.util.MyConstants;
import com.jf.apply_car.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/userinfo")
public class UserinfoController {
    @Autowired
    private UserinfoService userinfoService;

    @GetMapping("/list")
    public String list() {
        return "userinfo/userinfo-list";
    }

    @GetMapping("/edit")
    public String edit() {
        return "userinfo/userinfo-edit";
    }

    @GetMapping("/password")
    public String password() {
        return "userinfo/userinfo-password";
    }

    @GetMapping("/password/check")
    @ResponseBody
    public ResultJson passwordCheck(Userinfo userinfo) {
        return userinfoService.checkPassword(userinfo);
    }

    @PostMapping("/password")
    @ResponseBody
    public ResultJson password(Userinfo userinfo) {
        userinfoService.updatePassword(userinfo);
        return ResultJson.success();
    }

    @GetMapping("/reset/{userinfoId}")
    @ResponseBody
    public ResultJson reset(@PathVariable int userinfoId) {
        Userinfo userinfo = new Userinfo();
        userinfo.setUserinfoId(userinfoId);
        userinfo.setPassword(MyConstants.DEFAULT_PASSWORD);
        userinfoService.updatePassword(userinfo);
        return ResultJson.success();
    }

    @GetMapping("/page")
    @ResponseBody
    public ResultJson page(int page, int limit, Userinfo userinfo) {

        return userinfoService.getPage(page, limit, userinfo);
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResultJson delete(int[] ids) {
        userinfoService.delete(ids);
        return ResultJson.success();
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultJson update(Userinfo userinfo, Driver driver) {
        userinfoService.saveOrUpdate(userinfo, driver);
        return ResultJson.success();
    }

    @GetMapping("/info/{userinfoId}")
    @ResponseBody
    public ResultJson info(@PathVariable int userinfoId) {
        return userinfoService.getById(userinfoId);
    }


    @GetMapping("/driver/select")
    @ResponseBody
    public ResultJson driverSelect() {
        return userinfoService.driverSelect();
    }

}
